{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | This module is more or less a test module for development. 
--	 This module will not exist in it's current format for the library

import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as L8
import Data.ByteString (ByteString)

import Data.Binary.Get
import qualified Data.Binary.Get as G
import Data.Int (Int64)

import Data.Label -- Needs 'fclabels' package
import Prelude hiding ((.), id)
import Control.Category ((.), id)

import LibPE.MzHeader
import LibPE.DOSStub -- may change this to DosStub for consistency
import LibPE.PEHeader
import LibPE.SectionHeader
import LibPE.ParseRawSections

data PE32 = PE32
	{ _mzHeader :: MzHeader
	, _dosStub :: DOSStub
	, _peHeader :: PeHeader
	, _secHeader :: SectionHeader
	, _secRaw :: RawSections
	, _rest :: L.ByteString
	} deriving (Show, Eq, Ord)

mkLabel ''PE32

parsePE32 = do
	pe32_mzheader <- parseMzHeader

	let offSetToPE = fromIntegral $ get lfanew pe32_mzheader :: Int64
	currentOffSet <- bytesRead

	pe32_dosstub <- parseDosStub (offSetToPE - currentOffSet)
	pe32_peheader <- parsePeHeader

	-- Need to identify how many sections we need to recusively get
	let secNum = fromIntegral $ get (numOfSections . fileHdr) pe32_peheader :: Int
	
	pe32_secHeader <- parseSecHeader secNum

	pe32_secRaw <- parseRawSections $ get sectionList pe32_secHeader

	pe32_rest <- getRemainingLazyByteString

	return $ PE32 pe32_mzheader pe32_dosstub pe32_peheader pe32_secHeader pe32_secRaw pe32_rest

-- TESTING

input = "..\\..\\hexena-test\\test\\simple.exe"

-- peFile :: FilePath -> IO PE32
peFile :: FilePath -> IO PE32
peFile f =
	L.readFile f >>= \fh -> return $ runGet parsePE32 fh

-- simpleFile :: IO PE32
simpleFile :: IO PE32
simpleFile = peFile input 

{-
 -- peFile :: FilePath -> IO PE32
peFile :: FilePath -> IO (PE32, L.ByteString, Int64)
peFile f =
	L.readFile f >>= \fh -> return $ runGetState parsePE32 fh 0

-- simpleFile :: IO PE32
simpleFile :: IO (PE32, L.ByteString, Int64)
simpleFile = peFile input 
 -}

-- for section table take the amount from the offset to first header
-- I.E.
--		L.take (First Section - Current Offset) rest
